package com.android.geotrack;

import java.util.LinkedList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class TrackAdapter extends BaseAdapter
{
    private Context mContext;
    private LinkedList<Track> tracks;
    private LinkedList<Drawable> mThumbs;

    public TrackAdapter(Context mContext, Object tracks, Object mThumbs)
    {
        this.mContext = mContext;
        this.tracks = (LinkedList<Track>) tracks;
        this.mThumbs = (LinkedList<Drawable>) mThumbs;
    }

	public int getCount() {
        return mThumbs.size();
    }

    public Object getItem(int position)
    {
        return null;
    }

    public long getItemId(int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        
        // Click on imageView
        final int pos = position;
        imageView.setOnClickListener(new ImageView.OnClickListener(){
        	public void onClick(View v) {
        		Intent i = new Intent(mContext, TrackDetailsActivity.class);
        		i.putExtra("title", tracks.get(pos).getTitle());
        		i.putExtra("artist", tracks.get(pos).getArtist());
        		i.putExtra("thumb", tracks.get(pos).getThumbnail_medium());
        		i.setFlags( i.FLAG_ACTIVITY_NEW_TASK );
        		mContext.startActivity(i);
        	}	
        });
        
        imageView.setImageDrawable(mThumbs.get(position));
        
        return imageView;
    }
}